package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 */
@Service
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Resource
    private SetMealDishMapper mealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO 前端传过来的菜品
     */

    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        //1,向菜品表插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //这里能获取到新增的菜品的id值得益于useGeneratedKeys和keyProperty属性
//        <insert id="insert" useGeneratedKeys="true" keyProperty="id">
//                insert into dish (name, category_id, price, image, description, status, create_time, update_time, create_user, update_user, status)
//        values
//                (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status})
//    </insert>
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //2,向菜品口味表插入任意条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //1,判断当前菜品是否能够删除-是否存在起售中的菜品
        ids.forEach(id->{
          Dish dish = dishMapper.getDishById(id);
          if (dish.getStatus()== StatusConstant.ENABLE) {
              //说明当前菜品属于起售中
              throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
          }
        });
        //2,判断当前菜品是否能够删除-是否被套餐关联
        List<Long> setMealsIds = mealDishMapper.getSetMealIdsByDishIds(ids);
        if (setMealsIds!=null && setMealsIds.size()>0) {
            //当前菜品被套餐关联，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //跟据菜品id集合批量删除菜品数据
        dishMapper.deleteByIds(ids);
        //跟据菜品id集合批量删除菜品口味数据
        dishFlavorMapper.deleteByDishIds(ids);
    }

    //跟据id查询带有菜品口味的菜品数据
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getDishById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        //将查询出的数据封装成VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //修改菜品基本信息数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //删除原有口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //重新插入菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }
}
